﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MusicaTangible
{
    public partial class MainWindow : Form
    {
        private MusicProcessor musicProcessor;
        private PictureBox circleRed, circleOrange, circleYellow, circleGreen, circleLightblue, circleBlue, circlePurple;
        private PictureBox circle, quad, triangle, cross;
        private PictureBox hand;

        public MainWindow()
        {
            InitializeComponent();
            this.GotoFullScreen();
            this.CreatePictureBoxes();
            this.musicProcessor = new MusicProcessor(this);
        }

        private void GotoFullScreen()
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.Left = 0;
            this.Top = 0;
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.Height = Screen.PrimaryScreen.Bounds.Height;
        }

        public void CreatePictureBoxes()
        {
            #region Hand
            this.hand = new PictureBox();
            this.hand.Size = new System.Drawing.Size(47, 68);
            this.hand.Image = global::MusicaTangible.Properties.Resources.hand;
            this.hand.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.hand.Anchor = AnchorStyles.None;
            #endregion
            #region Notes

            this.circleRed = new PictureBox();
            this.circleRed.Size = new System.Drawing.Size(114, 112);
            this.circleRed.Image = global::MusicaTangible.Properties.Resources.token_c;
            this.circleRed.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.circleRed.Anchor = AnchorStyles.None;

            this.circleOrange = new PictureBox();
            this.circleOrange.Size = new System.Drawing.Size(114, 112);
            this.circleOrange.Image = global::MusicaTangible.Properties.Resources.token_d;
            this.circleOrange.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.circleOrange.Anchor = AnchorStyles.None;

            this.circleYellow = new PictureBox();
            this.circleYellow.Size = new System.Drawing.Size(114, 112);
            this.circleYellow.Image = global::MusicaTangible.Properties.Resources.token_e;
            this.circleYellow.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.circleYellow.Anchor = AnchorStyles.None;

            this.circleGreen = new PictureBox();
            this.circleGreen.Size = new System.Drawing.Size(114, 112);
            this.circleGreen.Image = global::MusicaTangible.Properties.Resources.token_f;
            this.circleGreen.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.circleGreen.Anchor = AnchorStyles.None;

            this.circleLightblue = new PictureBox();
            this.circleLightblue.Size = new System.Drawing.Size(114, 112);
            this.circleLightblue.Image = global::MusicaTangible.Properties.Resources.token_g;
            this.circleLightblue.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.circleLightblue.Anchor = AnchorStyles.None;

            this.circleBlue = new PictureBox();
            this.circleBlue.Size = new System.Drawing.Size(114, 112);
            this.circleBlue.Image = global::MusicaTangible.Properties.Resources.token_a;
            this.circleBlue.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.circleBlue.Anchor = AnchorStyles.None;

            this.circlePurple = new PictureBox();
            this.circlePurple.Size = new System.Drawing.Size(114, 112);
            this.circlePurple.Image = global::MusicaTangible.Properties.Resources.token_b;
            this.circlePurple.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.circlePurple.Anchor = AnchorStyles.None;

            #endregion
            #region Shapes

            this.quad = new PictureBox();
            this.quad.Size = new System.Drawing.Size(114, 112);
            this.quad.Image = global::MusicaTangible.Properties.Resources.quad;
            this.quad.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.quad.Anchor = AnchorStyles.None;

            this.circle = new PictureBox();
            this.circle.Size = new System.Drawing.Size(114, 112);
            this.circle.Image = global::MusicaTangible.Properties.Resources.circle;
            this.circle.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.circle.Anchor = AnchorStyles.None;

            this.triangle = new PictureBox();
            this.triangle.Size = new System.Drawing.Size(114, 112);
            this.triangle.Image = global::MusicaTangible.Properties.Resources.triangle;
            this.triangle.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.triangle.Anchor = AnchorStyles.None;

            this.cross = new PictureBox();
            this.cross.Size = new System.Drawing.Size(114, 112);
            this.cross.Image = global::MusicaTangible.Properties.Resources.cross;
            this.cross.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.cross.Anchor = AnchorStyles.None;

            #endregion

            for (int i = 0; i < this.tableLayoutPanel1.RowCount; i++)
            {
                PictureBox pp = new PictureBox();
                pp.Size = new System.Drawing.Size(114, 112);
                pp.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
                pp.Anchor = AnchorStyles.None;
                pp.Image = null;
                this.tableLayoutPanel1.Controls.Add(pp, 0, i);

                for (int j = 1; j < this.tableLayoutPanel1.ColumnCount; j++)
                {
                    PictureBox pp1 = new PictureBox();
                    pp1.Size = new System.Drawing.Size(114, 112);
                    pp1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
                    pp1.Anchor = AnchorStyles.None;
                    pp1.Image = null;
                    this.tableLayoutPanel1.Controls.Add(pp1, j, i);
                }
            }

            for (int j = 0; j < this.tableLayoutPanel2.ColumnCount; j++)
            {
                PictureBox pp1 = new PictureBox();
                pp1.Size = new System.Drawing.Size(114, 112);
                pp1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
                pp1.Anchor = AnchorStyles.None;
                pp1.Image = null;
                this.tableLayoutPanel2.Controls.Add(pp1, j, 0);
            }

        }

        public void UpdateColumn(int current)
        {
            if (this.tableLayoutPanel2.InvokeRequired)
            {
                this.tableLayoutPanel2.Invoke(new UpdateColumnDelegate(this.UpdateColumn), current);
            }
            else
            {
                if (current == 0)
                    ((PictureBox)this.tableLayoutPanel2.GetControlFromPosition(8, 0)).Image = null;
                else
                    ((PictureBox)this.tableLayoutPanel2.GetControlFromPosition(current, 0)).Image = null;
                ((PictureBox)this.tableLayoutPanel2.GetControlFromPosition(current + 1, 0)).Image = global::MusicaTangible.Properties.Resources.hand;
            }
        }

        private delegate void UpdateColumnDelegate(int current);

        #region Event Handlers

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBoxIniciar.Image = global::MusicaTangible.Properties.Resources.iniciar_on;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBoxIniciar.Image = global::MusicaTangible.Properties.Resources.iniciar_off;
        }

        private void pictureBox2_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBoxDetener.Image = global::MusicaTangible.Properties.Resources.detener_on;
        }

        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBoxDetener.Image = global::MusicaTangible.Properties.Resources.detener_off;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void pictureBox3_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox3.Image = global::MusicaTangible.Properties.Resources.salir_on;
        }

        private void pictureBox3_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox3.Image = global::MusicaTangible.Properties.Resources.salir_off;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.musicProcessor.Play();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            this.musicProcessor.Stop();
        }

        private void MainWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.musicProcessor.Stop();
        }

        #endregion

        private void pictureBoxSpeedDown_Click(object sender, EventArgs e)
        {
            if (this.musicProcessor.Tempo > 1)
            {
                this.musicProcessor.Tempo--;
                this.lblTempo.Text = this.musicProcessor.Tempo.ToString();
            }
        }

        private void pictureBoxSpeedUp_Click(object sender, EventArgs e)
        {
            if (this.musicProcessor.Tempo < 15)
            {
                this.musicProcessor.Tempo++;
                this.lblTempo.Text = this.musicProcessor.Tempo.ToString();
            }
        }

        private delegate void UpdateInstrumensDelegate(int row, Shapes shape);

        public void UpdateInstruments(int row, Shapes shape)
        {
            if (this.tableLayoutPanel1.InvokeRequired)
            {
                this.tableLayoutPanel1.Invoke(new UpdateInstrumensDelegate(this.UpdateInstruments), row, shape);
            }
            else
            {

                PictureBox pp = (PictureBox)this.tableLayoutPanel1.GetControlFromPosition(0, row);
                switch (shape)
                {
                    case Shapes.circle:
                        pp.Image = global::MusicaTangible.Properties.Resources.circle;
                        break;
                    case Shapes.square:
                        pp.Image = global::MusicaTangible.Properties.Resources.quad;
                        break;
                    case Shapes.triangle:
                        pp.Image = global::MusicaTangible.Properties.Resources.triangle;
                        break;
                    case Shapes.cross:
                        pp.Image = global::MusicaTangible.Properties.Resources.cross;
                        break;
                    default:
                        pp.Image = null;
                        break;
                }
            }
        }

        private delegate void UpdateNotesDelegate(int row, int column, Colors color);

        public void UpdateNotes(int row, int column, Colors color)
        {
            if (this.tableLayoutPanel1.InvokeRequired)
            {
                this.tableLayoutPanel1.Invoke(new UpdateNotesDelegate(this.UpdateNotes), row, column, color);
            }
            else
            {
                PictureBox pp = (PictureBox)this.tableLayoutPanel1.GetControlFromPosition(column+1, row);
                switch (color)
                {
                    case Colors.red:
                        pp.Image = global::MusicaTangible.Properties.Resources.token_c;
                        break;
                    case Colors.orange:
                        pp.Image = global::MusicaTangible.Properties.Resources.token_d;
                        break;
                    case Colors.yellow:
                        pp.Image = global::MusicaTangible.Properties.Resources.token_e;
                        break;
                    case Colors.green:
                        pp.Image = global::MusicaTangible.Properties.Resources.token_f;
                        break;
                    case Colors.lightblue:
                        pp.Image = global::MusicaTangible.Properties.Resources.token_g;
                        break;
                    case Colors.blue:
                        pp.Image = global::MusicaTangible.Properties.Resources.token_a;
                        break;
                    case Colors.purple:
                        pp.Image = global::MusicaTangible.Properties.Resources.token_b;
                        break;
                    default:
                        pp.Image = null;
                        break;
                }
            }
        }

        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Add:
                    if (this.musicProcessor.Tempo < 12)
                    {
                        this.musicProcessor.Tempo++;
                        this.lblTempo.Text = this.musicProcessor.Tempo.ToString();
                    }
                    break;
                case Keys.Subtract:
                    if (this.musicProcessor.Tempo > 1)
                    {
                        this.musicProcessor.Tempo--;
                        this.lblTempo.Text = this.musicProcessor.Tempo.ToString();
                    }
                    break;
                case Keys.Space:
                case Keys.Enter:
                    if (this.musicProcessor.IsPlaying)
                        this.musicProcessor.Stop();
                    else
                        this.musicProcessor.Play();
                    break;
                default:
                    break;
            }
        }
    }
}
